<?php
class CategoryController extends  IndexController 
{
    public function init()
    {
		parent::init();
        $this->utl            = Yaf_Registry::get("utl");
        $this->CategoryModel  = new CategoryModel();
        $this->WebsiteModel   = new WebsiteModel();
        $this->UserLogsModel  = new LogsModel();
        $this->Session        = Yaf_Registry::get("session");
        $this->tree           = Yaf_Registry::get("tree");

    }
     
    public function listAction()
    {
        $category_list      = array();
        $category_array     = array();
        $sites_info         = $this->WebsiteModel->selectSites();
        $category_info      = $this->CategoryModel->getCategoryInfo();
        foreach ($category_info AS $k => $v) 
        {
            $category_list[$k]['catid']        = $v['catid'];
            $category_list[$k]['parentid']     = $v['parentid'];
            $category_list[$k]['arrparentid']  = $v['arrparentid'];
            $category_list[$k]['catname']      = $v['catname'];
            $category_list[$k]['arrchildid']   = $v['arrchildid'];
            $category_list[$k]['current_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            foreach ($sites_info  AS $ks => $vs) 
            {
                if ($v['siteid'] === $vs['siteid']) 
                {
                    $category_list[$k]['sites_name'] = $vs['name'];
                    $category_list[$k]['siteid'] = $vs['siteid'];
                }
            }
        }
        $this->getView()->assign("session_user",$this->Session->user_info);
        $this->getView()->assign("category_list",$category_list);
        $this->getView()->assign("sites_list",$sites_info);
    }

    public function delAction() 
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id)) 
        {
            $this->forward('Category', 'article');
            return;
        }
        $rs = $this->CategoryModel->del($id);
        $session_del = array();
        $user_info = $this->Session->user_info;
        foreach ($user_info as $k => $v) 
        {
            $session_del[$k]['user_id'] = $v['user_id'];
            $session_del[$k]['table_name'] = "category";
            $session_del[$k]['action'] = "del";
        }
        $this->UserLogsModel->addUser($session_del);
        header('Content-type:application/json;charset:utf8');
        if (false === $rs) 
        {
            exit('{"status":false,"info":"删除失败！"}');
            return;
        } 
        exit('{"status":true,"info":"删除成功"}');
        return true;                                                                  
    }

    public function editAction() 
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id)) 
        {
            $this->redirect('Category', 'article');
            return;
        }
        if ($this->getRequest()->isPost()) 
        {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            $rs = $this->CategoryModel->edit($post, $id);
            $user_info = $this->Session->user_info;
            $session_edit = array();
            foreach ($user_info as $k => $v) 
            {
                $session_edit[$k]['user_id'] = $v['user_id'];
                $session_edit[$k]['table_name'] = "category";
                $session_edit[$k]['action'] = "edit";
            }
            $this->UserLogsModel->addUser($session_edit);
            if (false === $rs) 
            {   
                 exit('{"status":false,"info":"编辑失败！"}');
                 return;
            }  
            //var_dump($rs);
            exit('{"status":true,"info":"编辑成功！"}');
        }
    }

    public function addAction() 
    {
        if ($this->getRequest()->isPost()) {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            $rs = $this->CategoryModel->add($post);
            $user_info = $this->Session->user_info;
            $session_add = array();
            foreach ($user_info as $k => $v) 
            {
                $session_add[$k]['user_id'] = $v['user_id'];
                $session_add[$k]['table_name'] = "category";
                $session_add[$k]['action'] = "add";
            }
            $this->UserLogsModel->addUser($session_add);
            if (false === $rs) 
            {   
                exit('{"status":false,"info":"添加失败！"}');
                return;
            }
            exit('{"status":true,"info":"添加成功！"}');
        }
    }  
}
?>